package com.yuhan.business.service;

import com.yuhan.business.entity.User;
import com.yuhan.business.repository.UserRepository;
import com.yuhan.common.base.BaseService;
import com.yuhan.common.datasources.DataSourceNames;
import com.yuhan.common.datasources.annotation.DataSource;
import com.yuhan.common.utils.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @ProjectName: yuhan
 * @Package: com.yuhan.business.service
 * @Description: java类作用描述
 * @Author: hejing
 * @CreateDate: 2019/9/3 14:11
 * @Version: 1.0
 */
@Service
public class UserService extends BaseService<UserRepository, User> {
    @Autowired
    UserRepository userRepository;

    /***
     * 动态分页
     * @param pageNumber
     * @param pageSize
     * @param user
     * @return
     */
    @DataSource(name = DataSourceNames.SECOND)
    public Page findPage(Integer pageNumber, Integer pageSize, User user) {
        Page page = new Page();
        page.setPageNumber(pageNumber);
        page.setPageSize(pageSize);
        String sql = "select * from cr_user cr where del_flag = 0";
        if (StringUtils.isNotBlank(user.getUsername())) {
            sql += "and cr.username like '%" + user.getUsername() + "%'";
        }
        List<Map> list = userRepository.findMap(page, sql);
        page.getContent().addAll(list);
        return page;
    }
}
